package utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import android.os.Environment;
import android.util.Log;

public class UserInfo_Properties {
	private static String linebreak = File.separator;

	public static HttpRequestAndResponse http$ = new HttpRequestAndResponse();
	public static final String LAUNCHER_CMD = "am instrument --user 0 -w com.yiqijiao.test/testSuiteAndRunner.Runner";

	public static final String TAG = "yiqijiao";// 日志标签
	public static final int NUMBEROFRETRIES = 0;// 测试用例失败，重试次数

	public static final String URL$ = "";// 服务器地址
	public static final String URL_reBox = "";// 资源盒子服务器地址
	public static String TestCode = "1234";// 验证码

	public static final String MANAGERTEACHER_SESSION_ID = "managerTeacher_session_id";// 班级管理员的session_id
	public static final String MANAGERTEACHER_USER_ID = "managerTeacher_user_id";// 班级管理员的user_id
	public static final String TEACHER_USER_ID = "Teacher_user_id";// 非班级管理员的老师的user_id
	public static final String TEACHER_SESSION_ID = "Teacher_session_id";// 非班级管理员的老师的session_id
	public static final String MANAGERPARENT_USER_ID = "managerparent_user_id";// 主家长的user_id
	public static final String MANAGERPARENT_SESSION_ID = "managerparent_session_id";// 主家长的session_id
	public static final String PARENT_USER_ID = "parent_user_id"; // 非主家长的user_id
	public static final String PARENT_SESSION_ID = "parent_session_id";// 非主家长的session_id
	public static final String STUDENT_ID = "student_id";// 主家长下的孩子的id
	public static final String STUDENT_ID_TWO = "student_id_two";// 主家长下的孩子的id
	public static final String CLAZZ_ID = "clazz_id";// 班级管理员所在的班级id

	public static final String ANOTHERPARENT_PHONENUMBER = "ANOTHERPARENT_PHONENUMBER";;// 测试家长账户
	public static final String MANERGERPARENT_PHONENUMBER = "MANERGERPARENT_PHONENUMBER";// 测试家长账户

	public static final String MANERGERTEACHER_PHONENUMBER = "MANERGERTEACHER_PHONENUMBER";// 班级管理员账户
	public static final String ANOTHERTEACHER_PHONENUMBER = "ANOTHERTEACHER_PHONENUMBER";// 测试老师账号
	public static final String MANERGERTEACHERNAME = "manergerteachername";// 测试老师姓名

	public static final String MANERGERPARENTNAME = "manergerparentname";// 测试家长姓名
	public static final String STUDENTNAME = "studentname";// 加入班级小孩姓名
	public static final String STUDENTNAME$ = "studentname$";// 加入班级小孩姓名
	public static final String IDENTITY_NAME = "identity_name";// 孩子与家长的关系
	public static final String CLASSNAME = "classname";// 加入班级名称

	/**
	 * 判断api配置文件是否存在。如果不存在，则创建。如果存在则先删除再创建<br>
	 * 该方法必须且只能放在测试集的最前端。
	 * 
	 * @throws IOException
	 */
	public static void propertiesIsExsit() throws IOException {
		File file$ = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + linebreak + "MyTEST");
		file$.mkdirs();

		File file = new File(file$ + linebreak + "ApisAndUserInfo.properties");

		if (!file.exists()) {
			file.createNewFile();
		}

		FileReader fr = new FileReader(file);
		Properties p = new Properties();
		p.load(fr);
		// 定义配置文件键值对的key值，方便统一管理。其在使用save方法保存时必须使用这里定义的key值
		p.setProperty(MANAGERTEACHER_SESSION_ID, "");
		p.setProperty(MANAGERTEACHER_USER_ID, "");
		p.setProperty(TEACHER_USER_ID, "");
		p.setProperty(TEACHER_SESSION_ID, "");
		p.setProperty(MANAGERPARENT_USER_ID, "");
		p.setProperty(MANAGERPARENT_SESSION_ID, "");
		p.setProperty(PARENT_USER_ID, "");
		p.setProperty(PARENT_SESSION_ID, "");
		p.setProperty(STUDENT_ID, "");
		p.setProperty(STUDENT_ID_TWO, "");
		p.setProperty(CLAZZ_ID, "");

		p.setProperty(MANERGERTEACHERNAME, "自动化测试");
		p.setProperty(MANERGERPARENTNAME, "黑旋风李逵");
		p.setProperty(STUDENTNAME, "测试小孩");
		p.setProperty(STUDENTNAME$, "第二个孩子");
		p.setProperty(IDENTITY_NAME, "干爹");
		p.setProperty(CLASSNAME, "测试工具专业（3）班");
		p.setProperty(ANOTHERPARENT_PHONENUMBER, "18311113333");
		p.setProperty(MANERGERPARENT_PHONENUMBER, "18311114444");
		p.setProperty(ANOTHERTEACHER_PHONENUMBER, "18311112222");
		p.setProperty(MANERGERTEACHER_PHONENUMBER, "18311110000");

		FileWriter fw = new FileWriter(file);
		try {
			p.store(fw, "4.0apis.properties");
		} catch (IOException e) {
			Log.d(TAG, "Store method is fail!!!");
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 向配置文件中写入数据.（保存一些如session_id,user_id等数据）
	 * 
	 * @param key
	 * @param value
	 * @throws IOException
	 */
	public static void save(String key, String value) throws IOException {

		File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + linebreak + "MyTEST"
				+ linebreak + "ApisAndUserInfo.properties");
		if (!file.exists()) {
			propertiesIsExsit();
		}
		FileReader fr = new FileReader(file);

		Properties p = new Properties();
		p.load(fr);// 加载读取流中的数据
		p.setProperty(key, value);// 修改读取流中的键值。如果不存在就添加

		FileWriter fw = new FileWriter(file);
		p.store(fw, "4.0ApisAndUserInfo.properties");// 保存到文件

		fr.close();
		fw.close();
	}

	/**
	 * 读取配置文件中的值
	 * 
	 * @param key
	 * @return
	 * @throws IOException
	 */
	public static String getValue(String key) {
		String value = null;
		File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + linebreak + "MyTEST"
				+ linebreak + "ApisAndUserInfo.properties");

		try {
			FileReader fr = new FileReader(file);
			Properties p = new Properties();
			p.load(fr);

			value = p.getProperty(key);
			fr.close();
		} catch (FileNotFoundException e) {
			Log.e(TAG, "配置文件未找到");
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		}
		return value;
	}
}
